package com.winning.pmph.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winning.pmph.entity.TermBetween;
import com.winning.pmph.entity.TermStandard;
import com.winning.pmph.entity.TermStandardHistory;
import com.winning.pmph.mapper.TermStandardHistoryMapper;
import com.winning.pmph.utils.Const;
import com.winning.pmph.vo.TermStandardVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class TermStandardHistoryService extends ServiceImpl<TermStandardHistoryMapper, TermStandardHistory> {

    /**
     * 通过页面解决完标准术语冲突后,存储标准术语历史版本
     *
     * @param termStandard   入标准术语库的术语
     * @param termStandardVo 冲突页面的入库术语,冲突页面的标准术语,合并后的标准术语
     */
    public void saveConflictVersion(TermStandard termStandard, TermStandardVo termStandardVo) {
        List<String> conflictStandardNameList = termStandardVo.getConflictStandardList().stream().map(TermStandard::getName).collect(Collectors.toList());
        termStandardVo.getConflictBetweenList().forEach(t -> {
            TermStandardHistory termStandardHistory = JSON.parseObject(formatBetweenSynonym(t),
                    TermStandardHistory.class);
            termStandardHistory.setVersion(termStandard.getVersion());
            termStandardHistory.setStandardId(termStandard.getId());
            termStandardHistory.setTermType(Const.BETWEEN_TERM);
            termStandardHistory.setId(null);
            save(termStandardHistory);
        });

        termStandardVo.getConflictStandardList().forEach(t -> {
            TermStandardHistory termStandardHistory = JSON.parseObject(JSON.toJSONString(t), TermStandardHistory.class);
            termStandardHistory.setVersion(termStandard.getVersion());
            termStandardHistory.setStandardId(termStandard.getId());
            termStandardHistory.setTermType(Const.STANDARD_CONFLICT_TERM);
            termStandardHistory.setId(null);
            save(termStandardHistory);
        });

        termStandardVo.getStandardTermList().forEach(t -> {
            TermStandardHistory termStandardHistory = JSON.parseObject(JSON.toJSONString(t), TermStandardHistory.class);
            termStandardHistory.setVersion(termStandard.getVersion());
            termStandardHistory.setStandardId(termStandard.getId());
            termStandardHistory.setTermType(Const.STANDARD_CONFLICT_NEW_TERM);
            // 当前标准术语包含在冲突术语中,冲突更新,当前标准术语不包含在冲突术语中,冲突创建
            if (termStandard.getName().equals(t.getName())) {
                if (conflictStandardNameList.contains(termStandard.getName())) {
                    termStandardHistory.setType(Const.CONFLICT_UPDATE);
                } else {
                    termStandardHistory.setType(Const.CONFLICT_CREATE);
                }
                termStandardHistory.setTermType(Const.WITH_SAME_STANDARD_TERM);
            }
            termStandardHistory.setId(null);
            save(termStandardHistory);
        });
    }
    /**
     * 自动更新标准术语版本历史记录
     *
     * @param termBetween  与标准术语合并的入库术语
     * @param termStandard 被合并后的标准术语
     */
    public void saveAutoUpdateHistory(TermBetween termBetween, TermStandard termStandard) {
        // 存储入库术语
        TermStandardHistory termStandardHistory = JSON.parseObject(formatBetweenSynonym(termBetween),
                TermStandardHistory.class);
        termStandardHistory.setVersion(termStandard.getVersion());
        termStandardHistory.setStandardId(termStandard.getId());
        termStandardHistory.setTermType(Const.BETWEEN_TERM);
        termStandardHistory.setId(null);
        save(termStandardHistory);

        // 存储标准术语
        termStandardHistory = JSON.parseObject(JSON.toJSONString(termStandard),
                TermStandardHistory.class);
        termStandardHistory.setStandardId(termStandard.getId());
        termStandardHistory.setType(Const.AUTOMATIC_UPDATE);
        termStandardHistory.setId(null);
        save(termStandardHistory);
    }

    /**
     * 把中间术语的synonym转为jsonarray,方便入历史表
     *
     * @param termBetween
     * @return
     */
    public String formatBetweenSynonym(TermBetween termBetween) {
        // 因term_between表synonym和term_standard_history表的synonym属性类型不一致,在这进行转换
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(termBetween));
        jsonObject.put("synonym", new JSONArray());
        if (StringUtils.isNotBlank(termBetween.getSynonym())) {
            JSONArray synonymArray = new JSONArray();
            Arrays.asList(termBetween.getSynonym().split(";")).forEach(synonymItem -> {
                // 深copy
                JSONObject synonymObject = JSON.parseObject(JSON.toJSONString(jsonObject
                        .getJSONArray("nameSource").getJSONObject(0)));
                synonymObject.put("value", synonymItem);
                synonymArray.add(synonymObject);
            });
            jsonObject.put("synonym", synonymArray);
        }
        return jsonObject.toJSONString();
    }
}
